package com.firefly.service.query.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.assembler.cms.ArticleAssembler;
import com.firefly.dao.dao.cms.mapper.ArticleMapper;
import com.firefly.dao.dao.cms.po.CategoryPo;
import com.firefly.dao.dao.cms.service.ArticleService;
import com.firefly.dao.dao.cms.service.CategoryService;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.vo.CommonListVo;
import com.firefly.dao.dto.cms.ArticleSearchDto;
import com.firefly.dao.dao.cms.po.ArticlePo;
import com.firefly.dao.vo.cms.ArticleVo;
import com.firefly.dao.vo.cms.ArticleListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 文章
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/20
 **/
@Service
public class ArticleQuery {
    private final ArticleAssembler articleAssembler;
    private final ArticleService articleService;
    private final CategoryService categoryService;
    private final ArticleMapper articleMapper;
    @Autowired
    public ArticleQuery(ArticleAssembler articleAssembler,ArticleService articleService,ArticleMapper articleMapper,CategoryService categoryService){
        this.articleAssembler = articleAssembler;
        this.articleService = articleService;
        this.articleMapper = articleMapper;
        this.categoryService = categoryService;
    }
    public CommonListVo getList(ArticleSearchDto request) {
        QueryWrapper<ArticlePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!request.key.isBlank(), "title", request.key).eq(request.cateid > 0,"cate_id",request.cateid).between(!request.startAt.isBlank() && !request.endAt.isBlank(),"publish_at",request.startAt,request.endAt).eq("del_flag", 0);
        Long count = articleService.count(queryWrapper);
        queryWrapper.last("limit " + request.offset + "," + request.pageSize).orderByDesc("sort", "id");
        List<ArticleListVo> list = articleMapper.getList(request);
        CommonListVo result = new CommonListVo();
        result.setList(list);
        result.setCount(count);
        return result;
    }

    public ArticleVo getDetail(CommonDto request){
        return articleAssembler.getVo(articleService.getById(request.getDetailId()));
    }

    // 根据栏目url查询栏目下所有文章
    public Map getListForUrl(String url){
        QueryWrapper<CategoryPo> queryWrapperCate = new QueryWrapper<>();
        queryWrapperCate.eq("url",url);
        CategoryPo categoryPo = categoryService.getOne(queryWrapperCate);
        QueryWrapper<ArticlePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","title","url").eq("cate_id",categoryPo.getId()).le("publish_at", LocalDateTime.now()).orderByDesc("sort","id");
        List list = articleService.list(queryWrapper);
        Map result = new HashMap<>();
        result.put("list",list);
        result.put("cate",categoryPo);
        return result;
    }
    // 根据url查文章详情
    public ArticlePo getDetail(String url){
        QueryWrapper<ArticlePo> queryWrapperCate = new QueryWrapper<>();
        queryWrapperCate.eq("url",url);
        return articleService.getOne(queryWrapperCate);
    }
}
